package com.mattidroid.spiegel;

import java.util.Locale;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class EditSettings extends Activity {
	
	// used to count active tabs
	int NoT = 0;
	boolean modMade = false;
	
	static final int LANG_GER = 0;
	static final int LANG_ENG = 1;
	
	static final int TAB_ON = 1;	
	static final int TAB_OFF = 0;	
	
	static final int CALL_MORE_SETTINGS = 333;

	GlobalClass Data;	// Define Object for access to global data
	
	// read locale settings from prefs and set accordingly
	void setLocale () {
		
		//Dynamic Override system settings LOCALE 
		//Need in all Classes
		GlobalClass GlobalData = ((GlobalClass)getApplicationContext());
	    String languageToLoad  = GlobalData.isLocaleString(GlobalData.overrideLocale);
	    GlobalClass.log(false, "EditSettings.setLocale Locale Setting to : " + languageToLoad);
	    
	    if (languageToLoad.equals("")) {
	    	// do nothing
	    	 GlobalClass.log(true, "applicationInit.setLocale Locale EMPTY");
	    }
	    else {
	    	//Override system settings LOCALE
		    Locale locale = new Locale(languageToLoad); 
		    Locale.setDefault(locale);
		    Configuration config = new Configuration();
		    config.locale = locale;
		    getBaseContext().getResources().updateConfiguration(config, 
		    getBaseContext().getResources().getDisplayMetrics());
	    }
	    
	}
	
	void getCurrentSettings(){
		
		// Point Object for access to global data to right context
	    GlobalClass Data = ((GlobalClass)getApplicationContext());
	    
	    // count Number of Tabs
	    NoT = 0;
	    
		// Language
	    GlobalClass.log(false, "getCurrentSettings - getLanguage :" + Data._selectLanguage);
		CheckBox CheckBoxLanguage = (CheckBox) findViewById(R.id.CheckBoxLanguage);	
		if (Data._selectLanguage == LANG_ENG){
			CheckBoxLanguage.setChecked(true);
		}
		else {
			CheckBoxLanguage.setChecked(false);
		}
		
		// Tabs
		CheckBox CheckStartTab = (CheckBox) findViewById(R.id.CheckStartTab);
		if (Data._isStartTab == TAB_ON) {
			CheckStartTab.setChecked(true);
			NoT++;	
		}
		else {
			CheckStartTab.setChecked(false);		
		}
			
		CheckBox CheckWirtschaftTab = (CheckBox) findViewById(R.id.CheckWirtschaftTab);
		if (Data._isWirtschaftTab == TAB_ON) {
			CheckWirtschaftTab.setChecked(true);
			NoT++;	
		}
		else {
			CheckWirtschaftTab.setChecked(false);		
		}		

		CheckBox CheckPolitikTab = (CheckBox) findViewById(R.id.CheckPolitikTab);
		if (Data._isPolitikTab == TAB_ON) {
			CheckPolitikTab.setChecked(true);
			NoT++;	
		}
		else {
			CheckPolitikTab.setChecked(false);		
		}
	
		CheckBox CheckSportTab = (CheckBox) findViewById(R.id.CheckSportTab);
		if (Data._isSportTab == TAB_ON) {
			CheckSportTab.setChecked(true);
			NoT++;	
		}
		else {
			CheckSportTab.setChecked(false);		
		}

		CheckBox CheckNetzweltTab = (CheckBox) findViewById(R.id.CheckNetzweltTab);
		if (Data._isNetzweltTab == TAB_ON) {
			CheckNetzweltTab.setChecked(true);
			NoT++;	
		}
		else {
			CheckNetzweltTab.setChecked(false);		
		}
		
		CheckBox CheckWissenschaftTab = (CheckBox) findViewById(R.id.CheckWissenschaftTab);
		if (Data._isWissenschaftTab == TAB_ON) {
			CheckWissenschaftTab.setChecked(true);
			NoT++;	
		}
		else {
			CheckWissenschaftTab.setChecked(false);		
		}
			
		CheckBox CheckPanoramaTab = (CheckBox) findViewById(R.id.CheckPanoramaTab);
		if (Data._isPanoramaTab == TAB_ON) {
			CheckPanoramaTab.setChecked(true);
			NoT++;	
		}
		else {
			CheckPanoramaTab.setChecked(false);		
		}
			
		CheckBox CheckTickerTab = (CheckBox) findViewById(R.id.CheckTickerTab);
		if (Data._isTickerTab == TAB_ON) {
			CheckTickerTab.setChecked(true);
			NoT++;	
		}
		else {
			CheckTickerTab.setChecked(false);		
		}		
			
		CheckBox CheckRubrikenTab = (CheckBox) findViewById(R.id.CheckRubrikenTab);
		if (Data._isRubrikenTab == TAB_ON) {
			CheckRubrikenTab.setChecked(true);
			NoT++;	
		}
		else {
			CheckRubrikenTab.setChecked(false);		
		}
		
	}
	
	/* Not needed anymore
	void setCurrentSettings(){ 
		
		GlobalClass Data = ((GlobalClass)getApplicationContext());
	    
	    // count Number of Tabs
	    NoT = 0;	    
	    
		// Language
		CheckBox CheckBoxLanguage = (CheckBox) findViewById(R.id.CheckBoxLanguage);	
		if (CheckBoxLanguage.isChecked()) {
				// urlLanguage = LANG_GER;
				Data._selectLanguage = LANG_GER;
		}
		else {
			// urlLanguage = LANG_ENG;
			Data._selectLanguage = LANG_ENG; 
			//setLanguage(urlLanguage);	
		}
		
		GlobalClass.log("getCurrentSettings - getLanguage :" + Data._selectLanguage);
		
		// Tab StartTup
		CheckBox CheckStartTab = (CheckBox) findViewById(R.id.CheckStartTab);	
		if (CheckStartTab.isChecked()) {
				Data._isStartTab = TAB_ON;
				NoT++;	
		}
		else {
			Data._isStartTab = TAB_OFF;
		}
		
		CheckBox CheckWirtschaftTab = (CheckBox) findViewById(R.id.CheckWirtschaftTab);
		if (CheckWirtschaftTab.isChecked()) {
			Data._isWirtschaftTab = TAB_ON;
			NoT++;	
		}
		else {
			Data._isWirtschaftTab = TAB_OFF;
		}	

		CheckBox CheckPolitikTab = (CheckBox) findViewById(R.id.CheckPolitikTab);
		if (CheckPolitikTab.isChecked()) {
			Data._isPolitikTab = TAB_ON;
			NoT++;	
		}
		else {
			Data._isPolitikTab = TAB_OFF;
		}	

		CheckBox CheckSportTab = (CheckBox) findViewById(R.id.CheckSportTab);
		if (CheckSportTab.isChecked()) {
			Data._isSportTab = TAB_ON;
			NoT++;	
		}
		else {
			Data._isSportTab = TAB_OFF;
		}

		CheckBox CheckNetzweltTab = (CheckBox) findViewById(R.id.CheckNetzweltTab);
		if (CheckNetzweltTab.isChecked()) {
			Data._isNetzweltTab = TAB_ON;
			NoT++;	
		}
		else {
			Data._isNetzweltTab = TAB_OFF;
		}
		
		CheckBox CheckWissenschaftTab = (CheckBox) findViewById(R.id.CheckWissenschaftTab);
		if (CheckWissenschaftTab.isChecked()) {
			Data._isWissenschaftTab = TAB_ON;
			NoT++;	
		}
		else {
			Data._isWissenschaftTab = TAB_OFF;
		}

		// new
		CheckBox CheckPanoramaTab = (CheckBox) findViewById(R.id.CheckPanoramaTab);
		if (CheckPanoramaTab.isChecked()) {
			Data._isPanoramaTab = TAB_ON;
			NoT++;	
		}
		else {
			Data._isPanoramaTab = TAB_OFF;
		}
		
		CheckBox CheckTickerTab = (CheckBox) findViewById(R.id.CheckTickerTab);
		if (CheckTickerTab.isChecked()) {
			Data._isTickerTab = TAB_ON;
			NoT++;	
		}
		else {
			Data._isTickerTab = TAB_OFF;
		}
		
		CheckBox CheckRubrikenTab = (CheckBox) findViewById(R.id.CheckRubrikenTab);
		if (CheckRubrikenTab.isChecked()) {
			Data._isRubrikenTab = TAB_ON;
			NoT++;	
		}
		else {
			Data._isRubrikenTab = TAB_OFF;
		}		
	}
	*/
	
	// Override to handle back key
	@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && (modMade == true)){
            AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
            alertbox.setMessage(this.getString(R.string.msg_areyousure));
            alertbox.setTitle(this.getString(R.string.good_bye));
            alertbox.setPositiveButton(this.getString(R.string.yes), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                    EditSettings.this.finish();
                }
            });

            alertbox.setNegativeButton(this.getString(R.string.no), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface arg0, int arg1) {
                }
            });
            alertbox.show();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }   	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.settings);
		GlobalClass.log(false, "EditSettings = " + "onCreate NoT: " + NoT);
		
		// first get current life application settings
	    GlobalClass Data = ((GlobalClass)getApplicationContext());
	    
		Data.getStoredSettings();
		getCurrentSettings();
		
	    // read locale settings from prefs and set accordingly
	    setLocale();
		
		final CheckBox CheckBoxLanguage = (CheckBox) findViewById(R.id.CheckBoxLanguage);
		CheckBoxLanguage.setOnClickListener(new OnClickListener() {
			
			// first get current life application settings
		    GlobalClass Data = ((GlobalClass)getApplicationContext());
		    
			public void onClick(View v) {
				modMade = true; // Detect any changes to loaded configuration
				 if (CheckBoxLanguage.isChecked()) {
				 	Data._selectLanguage = LANG_ENG;
				 }
		         else {
		        	 Data._selectLanguage = LANG_GER;
		         }
				GlobalClass.log(false,  "CheckBox setOnClickListener CheckBoxLanguage: " + Data._isStartTab);
			}
		});
		
		GlobalClass.log(false, "EditSettings = " + "onCreate NoT: " + NoT);
	
		// Analyse checkboxes and watch number of tabs !!!
		final CheckBox CheckStartTab = (CheckBox) findViewById(R.id.CheckStartTab);
		CheckStartTab.setOnClickListener(new OnClickListener() {
			
			// first get current life application settings
		    GlobalClass Data = ((GlobalClass)getApplicationContext());
		    
			public void onClick(View v) {
				modMade = true; // Detect any changes to loaded configuration				
				 if (CheckStartTab.isChecked()) {
					 NoT++;
				 	Data._isStartTab = TAB_ON;
				 }
		         else {
		        	 NoT--;
		        	 Data._isStartTab = TAB_OFF;
		         }
				GlobalClass.log(false,  "CheckBox setOnClickListener StartTab - NoT: " + NoT);
				if (NoT == 0){
					NotEnoughTabs();
					Data._isStartTab = TAB_ON;
					CheckStartTab.setChecked(true);
					NoT = 1;
				}
			}
		});

		final CheckBox CheckWirtschaftTab = (CheckBox) findViewById(R.id.CheckWirtschaftTab);
		CheckWirtschaftTab.setOnClickListener(new OnClickListener() {
			
			// first get current life application settings
		    GlobalClass Data = ((GlobalClass)getApplicationContext());
		    
			public void onClick(View v) {
				modMade = true; // Detect any changes to loaded configuration				
				 if (CheckWirtschaftTab.isChecked()) {
					 NoT++;
				 	Data._isWirtschaftTab = TAB_ON;
				 }
		         else {
		        	 NoT--;
		        	 Data._isWirtschaftTab = TAB_OFF;
		         }
				GlobalClass.log(false, "CheckBox setOnClickListener CheckWirtschaftTab - NoT: " + NoT);
				if (NoT == 0){
					NotEnoughTabs();
					Data._isWirtschaftTab = TAB_ON;
					CheckWirtschaftTab.setChecked(true);
					NoT = 1;
				}
			}
		});		

		final CheckBox CheckPolitikTab = (CheckBox) findViewById(R.id.CheckPolitikTab);
		CheckPolitikTab.setOnClickListener(new OnClickListener() {
			
			// first get current life application settings
		    GlobalClass Data = ((GlobalClass)getApplicationContext());
		    
			public void onClick(View v) {
				modMade = true; // Detect any changes to loaded configuration				
				 if (CheckPolitikTab.isChecked()) {
					 NoT++;
				 	Data._isPolitikTab = TAB_ON;
				 }
		         else {
		        	 NoT--;
		        	 Data._isPolitikTab = TAB_OFF;
		         }
				GlobalClass.log(false, "CheckBox setOnClickListener CheckPolitikTab - NoT: " + NoT);
				if (NoT == 0){
					NotEnoughTabs();
					Data._isPolitikTab = TAB_ON;
					CheckPolitikTab.setChecked(true);
					NoT = 1;
				}
			}
		});			

		final CheckBox CheckSportTab = (CheckBox) findViewById(R.id.CheckSportTab);
		CheckSportTab.setOnClickListener(new OnClickListener() {
			
			// first get current life application settings
		    GlobalClass Data = ((GlobalClass)getApplicationContext());
		    
			public void onClick(View v) {
				modMade = true; // Detect any changes to loaded configuration				
				 if (CheckSportTab.isChecked()) {
					 NoT++;
				 	Data._isSportTab = TAB_ON;
				 }
		         else {
		        	 NoT--;
		        	 Data._isSportTab = TAB_OFF;
		         }
				GlobalClass.log(false, "CheckBox setOnClickListener CheckSportTab - NoT: " + NoT);
				if (NoT == 0){
					NotEnoughTabs();
					Data._isSportTab = TAB_ON;
					CheckSportTab.setChecked(true);
					NoT = 1;
				}
			}
		});		
	
		final CheckBox CheckNetzweltTab = (CheckBox) findViewById(R.id.CheckNetzweltTab);
		CheckNetzweltTab.setOnClickListener(new OnClickListener() {
			
			// first get current life application settings
		    GlobalClass Data = ((GlobalClass)getApplicationContext());
		    
			public void onClick(View v) {
				modMade = true; // Detect any changes to loaded configuration				
				 if (CheckNetzweltTab.isChecked()) {
					 NoT++;
				 	Data._isNetzweltTab= TAB_ON;
				 }
		         else {
		        	 NoT--;
		        	 Data._isNetzweltTab = TAB_OFF;
		         }
				GlobalClass.log(false, "CheckBox setOnClickListener CheckNetzweltTab - NoT: " + NoT);
				if (NoT == 0){
					NotEnoughTabs();
					Data._isNetzweltTab = TAB_ON;
					CheckNetzweltTab.setChecked(true);
					NoT = 1;
				}
			}
		});				

		final CheckBox CheckWissenschaftTab = (CheckBox) findViewById(R.id.CheckWissenschaftTab);
		CheckWissenschaftTab.setOnClickListener(new OnClickListener() {
			
			// first get current life application settings
		    GlobalClass Data = ((GlobalClass)getApplicationContext());
		    
			public void onClick(View v) {
				modMade = true; // Detect any changes to loaded configuration				
				 if (CheckWissenschaftTab.isChecked()) {
					 NoT++;
				 	Data._isWissenschaftTab= TAB_ON;
				 }
		         else {
		        	 NoT--;
		        	 Data._isWissenschaftTab = TAB_OFF;
		         }
				GlobalClass.log(false, "CheckBox setOnClickListener CheckWissenschaftTab - NoT: " + NoT);
				if (NoT == 0){
					NotEnoughTabs();
					Data._isWissenschaftTab = TAB_ON;
					CheckWissenschaftTab.setChecked(true);
					NoT = 1;
				}
			}
		});			

		final CheckBox CheckPanoramaTab = (CheckBox) findViewById(R.id.CheckPanoramaTab);
		CheckPanoramaTab.setOnClickListener(new OnClickListener() {
			
			// first get current life application settings
		    GlobalClass Data = ((GlobalClass)getApplicationContext());
		    
			public void onClick(View v) {
				modMade = true; // Detect any changes to loaded configuration				
				 if (CheckPanoramaTab.isChecked()) {
					 NoT++;
				 	Data._isPanoramaTab= TAB_ON;
				 }
		         else {
		        	 NoT--;
		        	 Data._isPanoramaTab = TAB_OFF;
		         }
				GlobalClass.log(false, "CheckBox setOnClickListener CheckPanoramaTab - NoT: " + NoT);
				if (NoT == 0){
					NotEnoughTabs();
					Data._isPanoramaTab = TAB_ON;
					CheckPanoramaTab.setChecked(true);
					NoT = 1;
				}
			}
		});			

		final CheckBox CheckTickerTab = (CheckBox) findViewById(R.id.CheckTickerTab);
		CheckTickerTab.setOnClickListener(new OnClickListener() {
			
			// first get current life application settings
		    GlobalClass Data = ((GlobalClass)getApplicationContext());
		    
			public void onClick(View v) {
				modMade = true; // Detect any changes to loaded configuration				
				 if (CheckTickerTab.isChecked()) {
					 NoT++;
				 	Data._isTickerTab= TAB_ON;
				 }
		         else {
		        	 NoT--;
		        	 Data._isTickerTab = TAB_OFF;
		         }
				GlobalClass.log(false, "CheckBox setOnClickListener CheckTickerTab - NoT: " + NoT);
				if (NoT == 0){
					NotEnoughTabs();
					Data._isTickerTab = TAB_ON;
					CheckTickerTab.setChecked(true);
					NoT = 1;
				}
			}
		});			

		final CheckBox CheckRubrikenTab = (CheckBox) findViewById(R.id.CheckRubrikenTab);
		CheckRubrikenTab.setOnClickListener(new OnClickListener() {
			
			// first get current life application settings
		    GlobalClass Data = ((GlobalClass)getApplicationContext());
		    
			public void onClick(View v) {
				modMade = true; // Detect any changes to loaded configuration				
				 if (CheckRubrikenTab.isChecked()) {
					 NoT++;
				 	Data._isRubrikenTab= TAB_ON;
				 }
		         else {
		        	 NoT--;
		        	 Data._isRubrikenTab = TAB_OFF;
		         }
				GlobalClass.log(false, "CheckBox setOnClickListener CheckRubrikenTab - NoT: " + NoT);
				if (NoT == 0){
					NotEnoughTabs();
					Data._isRubrikenTab = TAB_ON;
					CheckRubrikenTab.setChecked(true);
					NoT = 1;
				}
			}
		});			
								
		// Buttons from XML View
		final Button ButtonOkPressed = (Button) findViewById(R.id.ButtonOk);
		ButtonOkPressed.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// Perform action on clicks
				SaveOptions();
		    	EditSettings.this.finish();
			}
		});
		
		final Button ButtonChancelPressed = (Button) findViewById(R.id.ButtonCancel);
		ButtonChancelPressed.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// Perform action on clicks
				CancelOptions();
			}
		});
		
    }

 // Menu Function call
    
	public void SaveOptions()
	{
		// save settings temporary
		// not needed anymore due to immediate analysis in onClickListener
		// setCurrentSettings();
	    GlobalClass Data = ((GlobalClass)getApplicationContext());
		Data.saveStoredSettings();
		
		// say something
		Toast.makeText(this,this.getString(R.string.msg_save),Toast.LENGTH_SHORT).show();
		
		// Restart Application
		GlobalClass.log(false, "EditSetting.Save Options: Need to restart application");
		
		// This Restarts the Application !!!
		Intent RestartIntent = getBaseContext().getPackageManager()
        .getLaunchIntentForPackage( getBaseContext().getPackageName() );
		RestartIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(RestartIntent);
		
		
	}

	public void NotEnoughTabs()
	{
        AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
        alertbox.setMessage(this.getString(R.string.msg_atleastonetab));
        alertbox.setPositiveButton(this.getString(R.string.yes), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
            }
        });
        
        alertbox.show();
	}
	
	public void CancelOptions()
	{
		if (modMade == true) {
	        AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
	        alertbox.setMessage(this.getString(R.string.msg_areyousure));
	        alertbox.setPositiveButton(this.getString(R.string.yes), new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface arg0, int arg1) {
	                EditSettings.this.finish();
	            }
	        });
	
	        alertbox.setNegativeButton(this.getString(R.string.no), new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface arg0, int arg1) {
	            }
	        });
	        alertbox.show();			
		}
		else {
			EditSettings.this.finish();
		}
	}
    
	public void StartMoreOptions() {
		Intent myIntent = new Intent(this, MoreSettings.class); 
		startActivity(myIntent);
	}
	
	public void MoreOptions()
	{
		if (modMade == true) {
	        AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
	        alertbox.setMessage(this.getString(R.string.msg_cont_wo_save));
	        alertbox.setPositiveButton(this.getString(R.string.yes), new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface arg0, int arg1) {
	            	StartMoreOptions();
	            }
	        });
	
	        alertbox.setNegativeButton(this.getString(R.string.no), new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface arg0, int arg1) {
	            }
	        });
	        alertbox.show();
		}
		else {
			StartMoreOptions();
		}
	}
	
 // Options Menu using XML File
    
    // create menu
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.settings_menu, menu);
        return true;
    }
    
    // handle return from menu selection
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.save:
            SaveOptions();
        	this.finish();
            return true;       
        case R.id.cancel:
            CancelOptions();        	
            return true;
        case R.id.more:
            MoreOptions();        	
            return true;            
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    
    
}